package com.ruoyi.framework.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

@Component
public class SpringHelper implements ApplicationContextAware, EmbeddedValueResolverAware {
    //静态ApplicationContext
    private static ApplicationContext applicationContext;
    private static StringValueResolver valueResolver;

    public SpringHelper() {
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        /** 将框架初始化加载的ioc容器赋值给 静态ApplicationContext*/
        SpringHelper.applicationContext = applicationContext;
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {

    }

    //获取Ioc容器
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过class获取Bean
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    //通过名称获取Bean
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    //通过名称、class获取Bean
    public static <T> T getBean(Class<T> clazz, String name) {
        return getApplicationContext().getBean(name, clazz);
    }

}
